/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "events_init.h"
#include "user_cfg.h"
#include <stdio.h>
#include "lvgl.h"
#include "nrf_process.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif


#define SCREEN_STACK_SIZE 10
static lv_obj_t *screen_stack[SCREEN_STACK_SIZE];
static int stack_index = -1;

extern unsigned int counter;
extern char buf[4];

// 入栈操作
void push_screen(lv_obj_t *screen) {
    __disable_irq();
    if (stack_index < SCREEN_STACK_SIZE - 1) {
        stack_index++;
        screen_stack[stack_index] = screen;
    }
    __enable_irq();
}

// 出栈操作
lv_obj_t* pop_screen() {
    if (stack_index >= 0) {
        lv_obj_t *screen = screen_stack[stack_index];
        stack_index--;
        return screen;
    }
    return NULL;
}

// 获取当前页面
lv_obj_t* get_current_screen() {
    if (stack_index >= 0) {
        return screen_stack[stack_index];
    }
    return NULL;
}



static void main_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.main);
        ui_load_scr_animation(&guider_ui, &guider_ui.motion_1, guider_ui.motion_1_del, &guider_ui.main_del, setup_scr_motion_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

static void main_btn_2_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.main);
        ui_load_scr_animation(&guider_ui, &guider_ui.settings_1, guider_ui.settings_1_del, &guider_ui.main_del, setup_scr_settings_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

static void main_btn_3_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.main);
        ui_load_scr_animation(&guider_ui, &guider_ui.light_1, guider_ui.light_1_del, &guider_ui.main_del, setup_scr_light_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

static void main_btn_4_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.main);
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_1, guider_ui.massage_1_del, &guider_ui.main_del, setup_scr_massage_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

void events_init_main (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->main_btn_1, main_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_btn_2, main_btn_2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_btn_3, main_btn_3_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_btn_4, main_btn_4_event_handler, LV_EVENT_ALL, ui);
}

static void motion_1_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.motion_1);
        ui_load_scr_animation(&guider_ui, &guider_ui.motion_2, guider_ui.motion_2_del, &guider_ui.motion_1_del, setup_scr_motion_2, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

void events_init_motion_1 (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->motion_1_btn_1, motion_1_btn_1_event_handler, LV_EVENT_ALL, ui);
}

static void settings_1_btn_minus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {

        if(counter) counter--;
        sprintf(buf,  "%d", counter);
        lv_label_set_text(guider_ui.settings_1_label_value, buf);
        lv_bar_set_range(guider_ui.settings_1_bar_1, 0, 100);
        lv_bar_set_value(guider_ui.settings_1_bar_1, counter, LV_ANIM_OFF);
        Lcd_Backlight_Pwm(counter);
        break;
    }
    case LV_EVENT_LONG_PRESSED_REPEAT:
    {
        if(counter) counter--;
        sprintf(buf,  "%d", counter);
        // lv_textprogress_set_value(guider_ui.settings_1_textprogress_1, counter);
        lv_label_set_text(guider_ui.settings_1_label_value, buf);
        lv_bar_set_range(guider_ui.settings_1_bar_1, 0, 100);
        lv_bar_set_value(guider_ui.settings_1_bar_1, counter, LV_ANIM_OFF);
        Lcd_Backlight_Pwm(counter);
        break;
    }
    case LV_EVENT_RELEASED:
    {
        static uint16_t light_buf[1] = {0};
        light_buf[0] = counter;
        MHFLASH_Write(FLASH_BACKLIGHT_ADDR,(uint16_t*)light_buf,1);
        break;
    }
    default:
        break;
    }
}

static void settings_1_btn_plus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        if (counter<100)   counter++;
        sprintf(buf,  "%d", counter);
        lv_label_set_text(guider_ui.settings_1_label_value, buf);
        lv_bar_set_range(guider_ui.settings_1_bar_1, 0, 100);
        lv_bar_set_value(guider_ui.settings_1_bar_1, counter, LV_ANIM_OFF);
        Lcd_Backlight_Pwm(counter);
        break;
    }
    case LV_EVENT_LONG_PRESSED_REPEAT:
    {
        if (counter<100)   counter++;
        sprintf(buf,  "%d", counter);
        lv_label_set_text(guider_ui.settings_1_label_value, buf);
        lv_bar_set_range(guider_ui.settings_1_bar_1, 0, 100);
        lv_bar_set_value(guider_ui.settings_1_bar_1, counter, LV_ANIM_OFF);
        Lcd_Backlight_Pwm(counter);
        break;
    }

    case LV_EVENT_RELEASED:
    {
        static uint16_t light_buf[1] = {0};
        light_buf[0] = counter;
        MHFLASH_Write(FLASH_BACKLIGHT_ADDR,(uint16_t*)light_buf,1);
        break;
    }
    default:
        break;
    }
}


static void settings_1_btn_white_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        Ui_Theme_Swtich(&guider_ui,WHITE);

        break;
    }

    default:
        break;
    }

}

static void settings_1_btn_red_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        Ui_Theme_Swtich(&guider_ui,RED);
        break;
    }

    default:
        break;
    }

}

static void settings_1_btn_black_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        Ui_Theme_Swtich(&guider_ui,BLACK);
        break;
    }

    default:
        break;
    }

}

static void settings_1_btn_blue_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        Ui_Theme_Swtich(&guider_ui,BLUE);
        break;
    }

    default:
        break;
    }

}


void events_init_settings_1 (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->settings_1_btn_minus, settings_1_btn_minus_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_1_btn_plus, settings_1_btn_plus_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_1_btn_3, settings_1_btn_white_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_1_btn_4, settings_1_btn_red_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_1_btn_5, settings_1_btn_black_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_1_btn_6, settings_1_btn_blue_event_handler, LV_EVENT_ALL, ui);
}

static void massage_1_btn_4_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        if (backlight.bits.msg_on_off) {
            ui_load_scr_animation(&guider_ui, &guider_ui.massage_intensity, guider_ui.massage_intensity_del, &guider_ui.massage_1_del, setup_scr_massage_intensity, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        }
        break;
    }
    default:
        break;
    }
}

static void massage_1_btn_3_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        if (backlight.bits.msg_on_off) {
            ui_load_scr_animation(&guider_ui, &guider_ui.massage_mode, guider_ui.massage_mode_del, &guider_ui.massage_1_del, setup_scr_massage_mode, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        }
        break;
    }
    default:
        break;
    }
}

static void massage_1_btn_2_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        if (backlight.bits.msg_on_off) {
            ui_load_scr_animation(&guider_ui, &guider_ui.massage_time, guider_ui.massage_time_del, &guider_ui.massage_1_del, setup_scr_massage_time, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        }
        break;
    }
    default:
        break;
    }
}

static void massage_1_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // push_screen(guider_ui.massage_1);
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_onoff, guider_ui.massage_onoff_del, &guider_ui.massage_1_del, setup_scr_massage_onoff, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        break;
    }
    default:
        break;
    }
}

void events_init_massage_1 (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->massage_1_btn_4, massage_1_btn_4_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->massage_1_btn_3, massage_1_btn_3_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->massage_1_btn_2, massage_1_btn_2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->massage_1_btn_1, massage_1_btn_1_event_handler, LV_EVENT_ALL, ui);
}



// 处理物理按键按下事件
static void back_button_async_handler(void *param)
{
    if (&guider_ui == NULL) return; // 检查UI对象有效性
    lv_obj_t *current_scr = lv_scr_act();
    if (current_scr == NULL) return; // 检查当前屏幕有效性

    nrf_para.nrf.stop_send_flag = true;
    TimeOut_Record(&nrf_para.tout.release_tout, 200);

    if (current_scr == guider_ui.motion_1 && guider_ui.main != NULL && setup_scr_main != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, NULL, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.motion_2 && guider_ui.motion_1 != NULL && setup_scr_motion_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.motion_1, guider_ui.motion_1_del, &guider_ui.main_del, setup_scr_motion_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.massage_1 && guider_ui.main != NULL && setup_scr_main != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, NULL, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.settings_1 && guider_ui.main != NULL && setup_scr_main != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, NULL, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.massage_intensity && guider_ui.massage_1 != NULL && setup_scr_massage_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_1, guider_ui.massage_1_del, &guider_ui.main_del, setup_scr_massage_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.massage_mode && guider_ui.massage_1 != NULL && setup_scr_massage_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_1, guider_ui.massage_1_del, &guider_ui.main_del, setup_scr_massage_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.massage_onoff && guider_ui.massage_1 != NULL && setup_scr_massage_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_1, guider_ui.massage_1_del, &guider_ui.main_del, setup_scr_massage_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.massage_time && guider_ui.massage_1 != NULL && setup_scr_massage_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.massage_1, guider_ui.massage_1_del, &guider_ui.main_del, setup_scr_massage_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    }else if (current_scr == guider_ui.enter_pair && guider_ui.enter_pair != NULL && setup_scr_enter_pair != NULL) {
        //ui_load_scr_animation(&guider_ui, &guider_ui.light_1, guider_ui.light_1_del, &guider_ui.main_del, setup_scr_light_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    }else if (current_scr == guider_ui.low_bat && guider_ui.low_bat != NULL && setup_scr_low_bat != NULL) {
        //ui_load_scr_animation(&guider_ui, &guider_ui.light_1, guider_ui.light_1_del, &guider_ui.main_del, setup_scr_light_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    } else if (current_scr == guider_ui.light_1 && guider_ui.light_1 != NULL && setup_scr_light_1 != NULL) {
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, NULL, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    }
}

void handle_physical_back_button(void)
{
    lv_async_call(back_button_async_handler, NULL);
}



void events_init(lv_ui *ui)
{

}
